package org.example;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;


@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Fork(1)
@Warmup(iterations = 3)
@Measurement(iterations = 10)
public class JMHMain {

    private int[] res;

    @Benchmark
    public void testIntEq() {
        res = intEq();
    }

    public int[] intEq() {
        final int num = 2000000;
        int coefficient = 1000;

        int[] arrEven = new int[num / 2 + 1];
        int[] arrOdd = new int[num / 2 + 1];

        int evenIndex = 0;
        int oddIndex = 0;

        for (int i = 1; i <= num; i++) {
            if (i % 2 == 0) {
                arrEven[evenIndex] = i * coefficient;
                evenIndex++;
            } else {
                arrOdd[oddIndex] = i * coefficient;
                oddIndex++;
            }
        }

        arrEven[evenIndex] = (num + 1) * coefficient;
        arrOdd[oddIndex] = (num + 1) * coefficient;

        int eqCounts = 0;
        int eqVal = 0;
        int neqCounts = 0;

        for (int i = 0; i <= num / 2; i++) {
            if (arrEven[i] == arrOdd[i]) {
                eqCounts++;
                eqVal = arrOdd[i];
            } else {
                neqCounts++;
            }
        }
        return new int[]{neqCounts, eqCounts, eqVal};
    }

    @TearDown(Level.Iteration)
    public void showRes() {
        System.out.printf("两个100万成员的数组中 不相等的个数：%d 相等的个数：%d 相等值：%d\t耗时：", res[0] , res[1], res[2]);
    }

    public static void main(String[] args) throws Exception {
        Options opt = new OptionsBuilder()
                .include(JMHMain.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }
}